<?php
/**
 * 后台节点(权限控制节点)模块
 * 
 */
class NodeAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
		Vendor('Common.Tree');
	}
	
	/**
	 * 列表
	 */
	public function index(){
		
		// 构建生成树中所需的数据
		$Node     = D('Node')->getAllNode();
		$array    = array();
		$childAdd = L('nod_add_child_title');
		$nodeModi = L('cfg_list_itf_btn_modify');
		$nodeDele = L('cfg_list_itf_btn_delete');
		$conDelet = L('nod_oth_dtis_delCon');
		$lang     = getLang();
		foreach($Node as $k => $r) {
			$r['title']   = dejsonByLang($r['langval'],$lang);
			$r['status']  = $r['status']==1 ? '<font color="red">√</font>' :'<font color="blue">×</font>';
			
			$sub          = $r['level']==2 ? 'href="#" class="btn"' : 'href="'.U("/Node/add/pid/".$r['id']).'" class="btn blue"';
			$r['addsub']  = '<a '.$sub.' title="'.$childAdd.'" ><i class="icon-plus"></i></a>';
			
			$etidurl      = U('Node/edit','id='.$r['id'].'&pid='.$r['pid']);  
			$edit         = 'href="'.$etidurl.'" class="btn blue"';
			$r['edit']    = '<a '.$edit.' title="'.$nodeModi.'" ><i class="icon-edit"></i></a>';
			
			$del          = $r['level']<1 ? 'class="btn"' : ' class="btn blue delAlone"'; 
			$r['del']     = '<a '.$del.'  title="'.$nodeDele.'" data-id="'.$r['id'].'"><i class="icon-remove"></i></a>';
			
			$r['levels']   = $r['level'];
			$r['level']   = L('nod_form_ele_level_'.$r['level']);
			$array[]      = $r;
		}

		$str  = "<tr class='tr level_\$levels'>
				    <td align='center'><input style='width:55px' type='text' value='\$sort' size='3' name='sort[\$id]'></td>
				    <td class='name'>\$spacer \$title</td> 
				    <td align='center'>\$level</td> 
				    <td align='center'>\$status</td> 
					<td align='center'>
						\$addsub \$edit \$del
					</td>
				  </tr>";

  		$Tree = new Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree(0, $str);

		$this->assign('html_tree',$html_tree);
		$this->display();
	}

	/**
	 * 添加
	 */
	public function add(){
		
		$NodeDB  = D("Node");
		$langArr = C("LANG_ARRAY");
		$lang    = getLang();
		if(IS_POST) {
			if($NodeDB->create()){
				$row = $this->_post("value");
				$NodeDB->langval = json_encode($row);
				$NodeDB->title   = $row[$lang];
				
				$level   = $NodeDB->level;
				$func    = $this->_post("func");
				$funlang = $_POST["func_lang"];
			    $id      = $NodeDB->add();
				
				wlog(1);
				$obj = $NodeDB->find($id); 
				if($level==1){
					$NodeDB->create_child_node($func,$funlang,$obj,$langArr);
				}			
				
				$this->assign("jumpUrl",U('/Node/index'));
				$this->success(L('cfg_form_tipmsg_addsucc'));
			}else{
				wlog(2,$NodeDB->getError());
				$this->error($NodeDB->getError());
			}	
		}else{
			$Nodes = $NodeDB->getAllNode("level in (0,1)");
			$pid   = $this->_get('pid','intval',0);
			$array = array();
			foreach($Nodes as $k => $r) {
				$r['id']         = $r['id'];
				$r['title']      = dejsonByLang($r['langval'],$lang);
				$r['name']       = $r['name'];
				$r['disabled']   = 'disabled' ;
				$array[$r['id']] = $r;
				if($r['id']==$pid){
					if($r['level']==0){
						$selectnode = $NodeDB->getModuleNode();
						$this->assign('selectnode',$selectnode);
					}elseif($r['level']==1){
						$selectnode = $NodeDB->geActionNode($r['name']);
						$this->assign('selectnode',$selectnode);	
					}
				}
			}
			
			$str    = "<option value='\$id' \$selected \$disabled >\$spacer \$title</option>";
			$Tree   = new Tree();
			$Tree->init($array);
			$selects = $Tree->get_tree(0, $str, $pid);
			$this->assign('select_categorys',$selects);
			
			$node    = $NodeDB->find($pid);
			$this->assign('node',$node);
			
			$this->assign('langs',$langArr);
			$this->display();
		}
	}

	/**
	 * 编辑
	 */
	public function edit(){
		$NodeDB  = D('Node');
		$langArr = C("LANG_ARRAY");
		$lang    = getLang();
		if(IS_POST) {
			if($NodeDB->create()){
				
				$row = $this->_post("value");
				$NodeDB->langval = json_encode($row);
				$NodeDB->title   = $row[$lang];
				
				$id      = $this->_param("id");
				$level   = $NodeDB->level;
				$func    = $this->_param("func");
				$funlang = $_POST["func_lang"];
				$NodeDB->save();
				
				wlog(1);//记录日志
				$node   = $NodeDB->find($id);
				if($level==1){
					$NodeDB->create_child_node($func,$funlang,$node,$langArr);
				}

				$this->assign("jumpUrl",U('/Node/index'));
				$this->success(L('cfg_form_tipmsg_editsucc'));
			}else{
				wlog(2,$NodeDB->getError());
				$this->error($NodeDB->getError());
			}
		}else{
			$id  = $this->_get('id','intval',0);
			$pid = $this->_get('pid','intval',0);
			if(empty($id)){
				$this->error(L('cfg_form_tipmsg_illgel'));
			}
			
			$rows = $NodeDB->getNode('id='.$id);
			$this->assign('node', $rows);

			$allNode = $NodeDB->getAllNode();
			$array   = array();
			$lang    = getLang();
			foreach($allNode as $k => $r) {
				$r['id']         = $r['id'];
				$r['title']      = dejsonByLang($r['langval'],$lang);
				$r['name']       = $r['name'];
				$r['disabled']   = $r['level']==2 ? 'disabled' : '';
				$array[$r['id']] = $r;
				if($r['id']==$pid){
					$mmame2 = $r['name'];
				}
				if($r['id'] == $id){
					if($r['level']==1){
						$selectnode = $NodeDB->getModuleNode($r['name']);
						$this->assign('selectnode',$selectnode);
					}elseif($r['level']==2){
						$selectnode = $NodeDB->geActionNode($mmame2,$r['name']);
						$this->assign('selectnode',$selectnode);
					}
				}
			}
			$str  = "<option value='\$id' \$selected \$disabled >\$spacer \$title</option>";
			$Tree = new Tree();
			$Tree->init($array);
			$select_categorys = $Tree->get_tree(0, $str, $pid);
			$this->assign('select_categorys',$select_categorys);

			$value = json_decode($rows['langval'],true);
			$vals  = array();
			foreach($langArr as $k=>$v){
				$title = $value[$k];
				$vals[$k] = array('v'=>$v,'title'=>$title);
			}
			$this->assign("lang",$vals);
			
			$this->display();
		}

	}
	
	/**
	 * 删除
	 */
	public function del(){
		$id = $this->_param('id','intval',0);
		if(!$id){
			$this->ajaxReturn(0,L('cfg_form_tipmsg_illgel'),0);
		}
		
		$NodeDB = D('Node');
		$info = $NodeDB -> getNode(array('id'=>$id),'id');
		if($NodeDB->childNode($info['id'])){
			$this->ajaxReturn(0,L('nod_oth_dtis_exChild'),0);
		}
		
		if($NodeDB->delNode('id='.$id)){
			wlog(1,"",$id);
			$this->ajaxReturn(1,L('cfg_form_tipmsg_delsucc'),1);
		}else{
			wlog(2);
			$this->ajaxReturn(0,L('cfg_form_tipmsg_delfail'),0);
		}
	}

	/**
	 * 排序
	 */
	public function sort(){
		
		$sorts = $this->_POST('sort');
		if(!is_array($sorts))$this->error(L('cfg_form_tipmsg_illgel'));
		
		foreach ($sorts as $id => $sort) {
			D('Node')->upNode( array('id' =>$id , 'sort' =>intval($sort) ) );
		}
		$this->assign("jumpUrl",U('Node/index'));
		$this->success(L('nod_oth_sort_succ'));
	}

	
}